/*
** make_env.c for mysh in /home/nadali_t//local/svn/tek2ed/minishell1
** 
** Made by thomas nadalie
** Login   <nadali_t@epitech.net>
** 
** Started on  Thu Nov  8 12:23:38 2012 thomas nadalie
** Last update Thu Nov  8 12:26:30 2012 thomas nadalie
*/

#include	<stdlib.h>
#include	"my.h"

int		find_path(char *s1, char *s2)
{
  int		i;

  i = 0;
  while (s2[i])
    {
      if (s1[i] != s2[i])
	return (1);
      i++;
    }
  return (0);
}

char		*make_path(char *env)
{
  char		*path;
  int		i;
  int		j;

  i = 0;
  j = 0;
  while (env[i])
    {
      if ((path = malloc(sizeof(*env) * my_strlen(env) + 1)) == NULL)
	return (NULL);
      while (env[i] != '=')
	i++;
      i++;
      while (env[i] != '\n' && env[i] != '\0')
	{
	  path[j] = env[i];
	  j++;
	  i++;
	}
    }
  path[j] = '\0';
  return (path);
}

char		**concat_path(char **path, char *cmd)
{
  int		i;

  i = 1;
  while (path[i])
    {
      path[i] = my_concat(path[i], cmd);
      i++;
    }
  return (path);
}

char		**get_path(char **env)
{
  char		*path;
  char		**new_path;
  int		i;

  i = 0;
  while ((find_path(env[i], "PATH=")) == 1)
    i++;
  path = make_path(env[i]);
  new_path = my_path_to_wordtab(path);
  return (new_path);
}
